/*
 *  Copyright (C) 2010 Takaji
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * TransactionTypeManager.java
 *
 * Created on Jan 2, 2010, 1:27:03 PM
 */
package dakside.hacc.modules.transactions.trxtypeman;

import dakside.hacc.core.models.TransactionAction;
import dakside.hacc.core.models.TransactionType;
import java.awt.Component;
import java.awt.event.KeyEvent;
import javax.swing.DefaultListModel;
import org.dakside.duck.plugins.AppConstants;
import org.dakside.duck.plugins.Command;
import org.dakside.duck.plugins.CommandInvoker;
import org.dakside.duck.plugins.DefaultCommandInvoker;
import org.dakside.duck.plugins.ToolbarContainer;

/**
 *
 * @author Takaji
 */
public class TransactionTypeManagerView extends javax.swing.JPanel implements ToolbarContainer {

    /** Creates new form TransactionTypeManager */
    public TransactionTypeManagerView() {
        initComponents();
        refresh(); //refresh the application for the first time
    }

    /**
     * Delete selected transaction type
     */
    private void deleteSelectedTransactionType() {
        if (listTransactionType.getSelectedIndex() > -1) {
            //delete selected transaction types
            TransactionType trxType = (TransactionType) listTransactionType.getSelectedValue();
            TransactionTypeController.getInstance().delete(trxType);
            refresh();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        customToolbar = new javax.swing.JToolBar();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        listTransactionType = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        transactionTypeDetailPanel1 = new dakside.hacc.modules.transactions.trxtypeman.TransactionTypeDetailPanel();
        btnSaveTransactionType = new javax.swing.JButton();
        btnDeleteTransactionType = new javax.swing.JButton();

        customToolbar.setFloatable(false);
        customToolbar.setRollover(true);
        customToolbar.setName("customToolbar"); // NOI18N

        jSplitPane1.setDividerLocation(200);
        jSplitPane1.setName("jSplitPane1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        listTransactionType.setModel(new DefaultListModel());
        listTransactionType.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listTransactionType.setName("listTransactionType"); // NOI18N
        listTransactionType.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listTransactionTypeValueChanged(evt);
            }
        });
        listTransactionType.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                listTransactionTypeKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(listTransactionType);

        jSplitPane1.setLeftComponent(jScrollPane1);

        jPanel1.setName("jPanel1"); // NOI18N

        transactionTypeDetailPanel1.setName("transactionTypeDetailPanel1"); // NOI18N

        btnSaveTransactionType.setText("Save Transaction Type");
        btnSaveTransactionType.setName("btnSaveTransactionType"); // NOI18N
        btnSaveTransactionType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveTransactionTypeActionPerformed(evt);
            }
        });

        btnDeleteTransactionType.setText("Delete Transaction Type");
        btnDeleteTransactionType.setName("btnDeleteTransactionType"); // NOI18N
        btnDeleteTransactionType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteTransactionTypeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnSaveTransactionType)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDeleteTransactionType)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(transactionTypeDetailPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(transactionTypeDetailPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSaveTransactionType)
                    .addComponent(btnDeleteTransactionType))
                .addContainerGap())
        );

        jSplitPane1.setRightComponent(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveTransactionTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveTransactionTypeActionPerformed
        //TODO save something here
        TransactionType trxType = transactionTypeDetailPanel1.getTransactionType();
        if (trxType != null) {
            TransactionTypeController.getInstance().save(trxType);
            System.out.println("Saved!");
//            JOptionPane.showMessageDialog(null, debug(trxType));
        }

        transactionTypeDetailPanel1.setTransactionType(null);
        refresh();
    }//GEN-LAST:event_btnSaveTransactionTypeActionPerformed

    private void btnDeleteTransactionTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteTransactionTypeActionPerformed
        deleteSelectedTransactionType();
    }//GEN-LAST:event_btnDeleteTransactionTypeActionPerformed

    private void listTransactionTypeValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listTransactionTypeValueChanged
        Object value = null;
        if ((value = listTransactionType.getSelectedValue()) instanceof TransactionType) {
            transactionTypeDetailPanel1.setTransactionType((TransactionType) value);
        }
    }//GEN-LAST:event_listTransactionTypeValueChanged

    private void listTransactionTypeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_listTransactionTypeKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
            deleteSelectedTransactionType();
        }
    }//GEN-LAST:event_listTransactionTypeKeyReleased

    void refresh() {
        //reload transaction type list
        DefaultListModel model = (DefaultListModel) listTransactionType.getModel();
        System.out.println("loading models");
        model.clear();
        TransactionType[] types = TransactionTypeController.getInstance().getAllTransactionType();
        System.out.println("Found: " + types.length);
        for (TransactionType type : types) {
            model.addElement(type);
        }
        System.out.println("List size: " + listTransactionType.getModel().getSize());
        //refresh transaction type details
        transactionTypeDetailPanel1.refresh();
    }

    Object debug(TransactionType type) {
        StringBuilder builder = new StringBuilder("<html>");
        builder.append("Code:" + type.getCode() + "<br/>");
        builder.append("Name:" + type.getName() + "<br/>");
        builder.append("---------------------------<br/>");
        for (TransactionAction action : type.getActionArray()) {
            builder.append(action.getActionString() + " - " + action.getToAccount() + "<br/>");
        }
        return builder;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDeleteTransactionType;
    private javax.swing.JButton btnSaveTransactionType;
    private javax.swing.JToolBar customToolbar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JList listTransactionType;
    private dakside.hacc.modules.transactions.trxtypeman.TransactionTypeDetailPanel transactionTypeDetailPanel1;
    // End of variables declaration//GEN-END:variables
    //TOOLBAR CONTAINER SUPPORT
    private Component[] toolbar;

    public Component[] getToolbarItems() {
        if (toolbar == null) {
            toolbar = customToolbar.getComponents();
        }
        return toolbar;
    }
    private DefaultCommandInvoker myInvoker = null;

    public final synchronized CommandInvoker getInvoker() {
        if (myInvoker == null) {
            myInvoker = new DefaultCommandInvoker();
        }
        myInvoker.add(AppConstants.Commands.REFRESH, new Command() {

            public Object invoke(Object[] args) {
                refresh();
                return true;
            }
        });

        myInvoker.add(AppConstants.Commands.NEW, new Command() {

            public Object invoke(Object[] args) {
                transactionTypeDetailPanel1.setTransactionType(null);
                return true;
            }
        });
        return myInvoker;
    }
    //END OF TOOLBAR CONTAINER SUPPORT
}
